package com.liy.framework.common.utils;

import java.util.zip.*;
import java.util.*;
import java.io.*;
import org.slf4j.*;

public class FileUtil
{
    private static final Logger log;
    
    public static File createNewFile(final String filePath) throws IOException {
        final File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        return file;
    }
    
    public static File writerFile(final String filePath, final InputStream inputStream) throws IOException {
        final File file = createNewFile(filePath);
        final FileOutputStream fileOutputStream = new FileOutputStream(file);
        final byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }
        fileOutputStream.close();
        inputStream.close();
        return file;
    }
    
    public static File writerFile(final File file, final InputStream inputStream) throws IOException {
        final FileOutputStream fileOutputStream = new FileOutputStream(file, false);
        final byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }
        fileOutputStream.close();
        inputStream.close();
        return file;
    }
    
    public static byte[] file2byte(final String filePath) {
        byte[] buffer = null;
        try {
            final File file = new File(filePath);
            final FileInputStream fis = new FileInputStream(file);
            final ByteArrayOutputStream bos = new ByteArrayOutputStream();
            final byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e2) {
            e2.printStackTrace();
        }
        return buffer;
    }
    
    public static byte[] file2byte(final File file) {
        byte[] buffer = null;
        try {
            final FileInputStream fis = new FileInputStream(file);
            final ByteArrayOutputStream bos = new ByteArrayOutputStream();
            final byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toString().getBytes("UTF-8");
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e2) {
            e2.printStackTrace();
        }
        return buffer;
    }
    
    public static byte[] file2byteForGZip(final File file) throws IOException {
        final byte[] b = file2byte(file);
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        final GZIPOutputStream gos = new GZIPOutputStream(os);
        gos.write(b);
        gos.finish();
        gos.flush();
        gos.close();
        os.flush();
        os.close();
        System.out.println(os.toString());
        return os.toByteArray();
    }
    
    public static void byte2File(final byte[] buf, final String filePath, final String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            final File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }
            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buf);
        }
        catch (Exception e) {
            e.printStackTrace();
            if (bos != null) {
                try {
                    bos.close();
                }
                catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                }
                catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
        finally {
            if (bos != null) {
                try {
                    bos.close();
                }
                catch (IOException e3) {
                    e3.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                }
                catch (IOException e3) {
                    e3.printStackTrace();
                }
            }
        }
    }
    
    public static void mkdir(final String dir) {
        try {
            final String dirTemp = dir;
            final File dirPath = new File(dirTemp);
            if (!dirPath.exists()) {
                dirPath.mkdir();
            }
        }
        catch (Exception e) {
            FileUtil.log.error("\u521b\u5efa\u76ee\u5f55\u64cd\u4f5c\u51fa\u9519: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void createNewFile(final String fileName, final String content) {
        try {
            final String fileNameTemp = fileName;
            final File filePath = new File(fileNameTemp);
            if (!filePath.exists()) {
                filePath.createNewFile();
            }
            final FileWriter fw = new FileWriter(filePath);
            fw.write(content);
            fw.flush();
            fw.close();
        }
        catch (Exception e) {
            FileUtil.log.error("\u65b0\u5efa\u6587\u4ef6\u64cd\u4f5c\u51fa\u9519: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void delFile(final String fileName) {
        try {
            final String filePath = fileName;
            final File delFile = new File(filePath);
            delFile.delete();
        }
        catch (Exception e) {
            FileUtil.log.error("\u5220\u9664\u6587\u4ef6\u64cd\u4f5c\u51fa\u9519: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void delFolder(final String folderPath) {
        try {
            delAllFile(folderPath);
            final String filePath = folderPath;
            final File myFilePath = new File(filePath);
            myFilePath.delete();
        }
        catch (Exception e) {
            FileUtil.log.error("\u5220\u9664\u6587\u4ef6\u5939\u64cd\u4f5c\u51fa\u9519" + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void delAllFile(final String path) {
        final File file = new File(path);
        if (!file.exists()) {
            return;
        }
        if (!file.isDirectory()) {
            return;
        }
        final String[] childFiles = file.list();
        File temp = null;
        for (int i = 0; i < childFiles.length; ++i) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + childFiles[i]);
            }
            else {
                temp = new File(path + File.separator + childFiles[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + childFiles[i]);
                delFolder(path + "/" + childFiles[i]);
            }
        }
    }
    
    public static void copyFile(final String srcFile, final String dirDest) {
        try {
            final FileInputStream in = new FileInputStream(srcFile);
            mkdir(dirDest);
            final FileOutputStream out = new FileOutputStream(dirDest + "/" + new File(srcFile).getName());
            final byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            in.close();
        }
        catch (Exception e) {
            FileUtil.log.error("\u590d\u5236\u6587\u4ef6\u64cd\u4f5c\u51fa\u9519:" + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void copyInputstreamToFile(final InputStream is, final File file) {
        try {
            final FileOutputStream out = new FileOutputStream(file);
            final byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            is.close();
        }
        catch (Exception e) {
            FileUtil.log.error("\u590d\u5236\u6587\u4ef6\u64cd\u4f5c\u51fa\u9519:" + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void copyFile(final String srcFile, final String dirDest, final String destFilePath) {
        try {
            final FileInputStream in = new FileInputStream(srcFile);
            mkdir(dirDest);
            final FileOutputStream out = new FileOutputStream(dirDest + "/" + destFilePath);
            final byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            in.close();
        }
        catch (Exception e) {
            FileUtil.log.error("\u590d\u5236\u6587\u4ef6\u64cd\u4f5c\u51fa\u9519:" + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void copyFolder(final String oldPath, final String newPath) {
        try {
            mkdir(newPath);
            final File file = new File(oldPath);
            final String[] files = file.list();
            File temp = null;
            for (int i = 0; i < files.length; ++i) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + files[i]);
                }
                else {
                    temp = new File(oldPath + File.separator + files[i]);
                }
                if (temp.isFile()) {
                    final FileInputStream input = new FileInputStream(temp);
                    final FileOutputStream output = new FileOutputStream(newPath + "/" + temp.getName().toString());
                    final byte[] buffer = new byte[2048];
                    int len;
                    while ((len = input.read(buffer)) != -1) {
                        output.write(buffer, 0, len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                if (temp.isDirectory()) {
                    copyFolder(oldPath + "/" + files[i], newPath + "/" + files[i]);
                }
            }
        }
        catch (Exception e) {
            FileUtil.log.error("\u590d\u5236\u6587\u4ef6\u5939\u64cd\u4f5c\u51fa\u9519:" + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void moveFile(final String oldPath, final String newPath) {
        copyFile(oldPath, newPath);
        delFile(oldPath);
    }
    
    public static void moveFiles(final String oldPath, final String newPath) {
        copyFolder(oldPath, newPath);
        delAllFile(oldPath);
    }
    
    public static void moveFolder(final String oldPath, final String newPath) {
        copyFolder(oldPath, newPath);
        delFolder(oldPath);
    }
    
    public static String zipFiles(final String srcDir, final String destDir) throws Exception {
        String tempFileName = null;
        final byte[] buf = new byte[2048];
        final File[] files = new File(srcDir).listFiles();
        if (files != null) {
            for (final File file : files) {
                if (file.isFile()) {
                    final FileInputStream fis = new FileInputStream(file);
                    final BufferedInputStream bis = new BufferedInputStream(fis);
                    if (destDir.endsWith(File.separator)) {
                        tempFileName = destDir + file.getName() + ".zip";
                    }
                    else {
                        tempFileName = destDir + "/" + file.getName() + ".zip";
                    }
                    final FileOutputStream fos = new FileOutputStream(tempFileName);
                    final BufferedOutputStream bos = new BufferedOutputStream(fos);
                    final ZipOutputStream zos = new ZipOutputStream(bos);
                    final ZipEntry ze = new ZipEntry(file.getName());
                    zos.putNextEntry(ze);
                    int len;
                    while ((len = bis.read(buf)) != -1) {
                        zos.write(buf, 0, len);
                        zos.flush();
                    }
                    bis.close();
                    zos.close();
                }
            }
        }
        return tempFileName;
    }
    
    public static String readData(final InputStream inSream, final String charsetName) throws Exception {
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        final byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inSream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        final byte[] data = outStream.toByteArray();
        outStream.close();
        inSream.close();
        return new String(data, charsetName);
    }
    
    public static Set<String> readFile(final String path) throws Exception {
        final Set<String> datas = new HashSet<String>();
        final FileReader fr = new FileReader(path);
        final BufferedReader br = new BufferedReader(fr);
        String line = null;
        while ((line = br.readLine()) != null) {
            datas.add(line);
        }
        br.close();
        fr.close();
        return datas;
    }
    
    public static String getJarFilePath(final String sourcePath, final Class clazz) throws IOException {
        final String systemTempDir = System.getProperty("java.io.tmpdir");
        final InputStream stream = clazz.getClassLoader().getResourceAsStream(sourcePath);
        final String destination = systemTempDir + File.separator + sourcePath;
        final File tmpFile = new File(destination);
        if (tmpFile.exists()) {
            return destination;
        }
        if (!tmpFile.getParentFile().exists()) {
            tmpFile.getParentFile().mkdirs();
        }
        final byte[] bytes = new byte[1024];
        final FileOutputStream downloadFile = new FileOutputStream(destination);
        int index;
        while ((index = stream.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        stream.close();
        downloadFile.close();
        return destination;
    }
    
    public static String getTmpdirFilePath(final String filePath) {
        return System.getProperty("java.io.tmpdir") + File.separator + filePath;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)FileUtil.class);
    }
}
